package org.example.jcclubsubjectapplicationcontroller.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.example.jcclubsubjectapplicationcontroller.convet.SubjectCategoryDTOConverter;
import org.example.jcclubsubjectapplicationcontroller.dto.SubjectCategoryDTO;
import org.example.jcclubsubjectcommon.entity.Result;
import org.example.jcclubsubjectdomain.basic.entity.SubjectCategoryBo;
import org.example.jcclubsubjectdomain.basic.service.SubjectCategoryDomaoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author 啊昌
 * @date 2025/4/12 11:15
 * SubjectCategoryController
 */


@RestController
@RequestMapping("/subject/category/")
@Slf4j
public class SubjectCategoryController {

	@Autowired
	private SubjectCategoryDomaoService subjectCategoryDomaoService;

	/**
	 * 新增分类
	 */
	@PostMapping("add")
	public Result<Boolean> add(@RequestBody SubjectCategoryDTO subjectCategoryDTO) {
		try {
//			log.isInfoEnabled()  表示日志为info 才会打印日志
			if(log.isInfoEnabled()){
				log.info("SubjectCategoryController.add.dto:{}", JSON.toJSONString(subjectCategoryDTO));
			}
//			Preconditions 使用第三方的 依赖 com.google.guava:guava: 19.0
//			用于断言返回错误信息
			SubjectCategoryBo subjectCategoryBo = SubjectCategoryDTOConverter.INSTANCE.convertDTOToBO(subjectCategoryDTO);
			Preconditions.checkNotNull(subjectCategoryDTO.getCategoryType(), "分类类型不能为空");
			Preconditions.checkArgument(StringUtils.isNotBlank(subjectCategoryDTO.getCategoryName()), "分类名称不能为空");
			Preconditions.checkNotNull(subjectCategoryDTO.getParentId(), "父级id不能为空");
			return  Result.ok(subjectCategoryDomaoService.insert(subjectCategoryBo));
		} catch (Exception e) {
			log.error("SubjectInfoController.add.error:{}", e.getMessage());
			return Result.fail(e.getMessage());
		}
	}


	/**
	 * 新增分类
	 */
	@PostMapping("queryPrimaryCategory")
	public Result<List<SubjectCategoryDTO>> queryPrimaryCategory(@RequestBody SubjectCategoryDTO subjectCategoryDTO) {
		try {
			if(log.isInfoEnabled()){
				log.info("SubjectCategoryController.queryPrimaryCategory.dto:{}", JSON.toJSONString(subjectCategoryDTO));
			}
//			将 dto 转换为 bo
			SubjectCategoryBo subjectCategoryBo = SubjectCategoryDTOConverter.INSTANCE.convertDTOToBO(subjectCategoryDTO);
//			查重大分类
			List<SubjectCategoryBo> subjectCategoryBOList = subjectCategoryDomaoService.queryPrimaryCategory(subjectCategoryBo);
//			将分类的bo list 转化成  SubjectCategoryDTO
			List<SubjectCategoryDTO> subjectCategoryDTOList = SubjectCategoryDTOConverter.INSTANCE.convertListBOToDTO(subjectCategoryBOList);
			return Result.ok(subjectCategoryDTOList);
		} catch (Exception e) {
			log.error("queryPrimaryCategory.add.error:{}", e.getMessage());
			return Result.fail(e.getMessage());
		}
	}
}
